/*
 * Copyright (c) 2020-2099 the original author or authors. All rights reserve.
 */
package org.tianyun.cloud.ext.i18n;

import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.tianyun.cloud.context.ServletContext;
import org.tianyun.cloud.context.ServletContext.ServletContextHolder;

import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;

/**
 * 语言国际化配置
 *
 * @auther ebert_chan
 */
@ConfigurationProperties(prefix = "i18n", ignoreInvalidFields = true)
public class I18nMessageSource {

    /**
     * 本地语言设置，默认中文简体(zh-CN)
     */
    private String locale;

    /**
     * 语言配置项，key为语言，value为配置项集合
     */
    private Map<String, Messages> messages = new HashMap<>();

    public String getLocale() {
        return locale == null || locale.length() == 0 ? Locale.SIMPLIFIED_CHINESE.toLanguageTag() : locale;
    }

    public void setLocale(String locale) {
        this.locale = locale;
    }

    public void setMessages(Map<String, Messages> messages) {
        this.messages = messages;
    }

    public Map<String, Messages> getMessages() {
        return messages;
    }

    /**
     * 获取响应码信息
     *
     * @param key
     * @param args
     * @return
     * @auther ebert_chan
     */
    public String getMessage(String key, Object... args) {
        String language = ServletContextHolder.getValue(ServletContext.CONTEXT_LANGUAGE);
        if (language == null) {
            language = this.getLocale();
        }

        return this.getMessage(language, key, args);
    }

    /**
     * 获取对应语言的响应码信息
     *
     * @param language
     * @param key
     * @param args
     * @return
     * @auther ebert_chan
     */
    public String getMessage(String language, String key, Object... args) {
        String value = null;
        if (this.messages == null || this.messages.isEmpty()) {
            return value;
        }

        Messages locale = this.getLocale(language);
        value = locale.getMessage(key);
        if (StringUtils.isNotBlank(value) && args != null && args.length > 0) {
            value = MessageFormat.format(value, args);
        }
        return value;
    }

    /**
     * 获取配置值
     *
     * @param key
     * @param args
     * @return
     * @auther ebert_chan
     */
    public String getProperty(String key, Object... args) {
        String language = ServletContextHolder.getValue(ServletContext.CONTEXT_LANGUAGE);
        if (language == null) {
            language = this.getLocale();
        }
        return this.getProperty(language, key, args);
    }

    /**
     * 获取对应语言的配置值
     *
     * @param language
     * @param key
     * @param args
     * @return
     * @auther ebert_chan
     */
    public String getProperty(String language, String key, Object... args) {
        String value = null;
        if (this.messages == null || this.messages.isEmpty()) {
            return value;
        }

        Messages locale = this.getLocale(language);
        value = locale.getProperty(key);
        if (StringUtils.isNotBlank(value) && args != null && args.length > 0) {
            value = MessageFormat.format(value, args);
        }
        return value;
    }

    private Messages getLocale(String language) {
        Messages local = this.messages.get(language);
        if (local == null && !getLocale().equals(language)) {
            local = this.messages.get(getLocale());
        }
        return local;
    }

    /**
     * 检查是否包含指定语言
     *
     * @param language
     * @return
     * @auther ebert_chan
     */
    public boolean containsLanguage(String language) {
        if (this.messages == null || this.messages.isEmpty()) {
            return Boolean.FALSE;
        }
        return this.messages.containsKey(language);
    }

    public static class Messages {

        /**
         * 响应码
         */
        private Properties response = new Properties();

        /**
         * 属性集合
         */
        private Properties properties = new Properties();

        public void setResponse(Properties response) {
            this.response = response;
        }

        public Properties getResponse() {
            return response;
        }

        public void setProperties(Properties properties) {
            this.properties = properties;
        }

        public Properties getProperties() {
            return properties;
        }

        /**
         * 获取响应消息
         *
         * @param key
         * @return
         * @auther ebert_chan
         */
        public String getMessage(String key) {
            String value = null;
            if (this.response == null || this.response.isEmpty()) {
                return value;
            }
            return response.getProperty(key);
        }

        /**
         * 获取配置消息
         *
         * @param key
         * @return
         * @auther ebert_chan
         */
        public String getProperty(String key) {
            String value = null;
            if (this.properties == null || this.properties.isEmpty()) {
                return value;
            }
            return properties.getProperty(key);
        }

    }

}
